package com.punegtug.opensocial.gifts.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import com.punegtug.opensocial.gifts.model.Gift;
import com.punegtug.opensocial.gifts.persistence.PMF;

@SuppressWarnings("serial")
public class GiftsServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("application/json");
		PersistenceManager pMgr = PMF.get().getPersistenceManager();
		List<Gift> gifts = (List<Gift>) pMgr.newQuery(Gift.class).execute();
		StringBuilder strBuilder=new StringBuilder("[");
		int count=0;
		for(Gift gift:gifts){
			if(count>0){
				strBuilder.append(",");
			}

			strBuilder.append("{");
			strBuilder.append("\"key\":\"").append(gift.getKey()).append("\"");
			strBuilder.append(",");
			strBuilder.append("\"name\":\"").append(gift.getName()).append("\"");
			strBuilder.append("}");
			count++;
		}
		strBuilder.append("]");
		resp.getWriter().println(strBuilder.toString());
	}
}
